let Sequelize = require('sequelize');
let Op = Sequelize.Op;
const University = require('./ORM');

module.exports = async (req, res) => {
  const { type = '', page = 1, province, schoolname = '' } = req.query;
  const pageSize = 10;
  // 查询条件
  let whereInfo = {
    type: {
      [Op.like]: `%${type}%`
    },
    schoolname: {
      [Op.like]: `%${schoolname}%`
    }
  };
  if (province && province !== 'null' && province !== 'undefined') {
    whereInfo.province = province;
  }
  // if(!schoolname && schoolname ==='null' && schoolname ==='undefined'){
  //   delete whereInfo.schoolname;
  // }
  await University.sync(); // 表初始化
  // 数据库查找
  const result = await University.findAndCountAll({
    order: [['id', 'ASC']], // 正常升序
    where: whereInfo,
    offset: (page - 1) * pageSize,
    limit: pageSize
  });
  const data = {
    result: result.rows,
    pagination: {
      page,
      pageSize,
      total: result.count
    }
  };
  if (result.rows) {
    res.api_success(data);
  } else {
    res.api_error('查找失败', 404, null);
  }
};
